﻿using System;
using System.Diagnostics;
using System.Collections;
using System.Threading;
using Microsoft.MediaCenter;
using Microsoft.MediaCenter.UI;
using System.IO;
using System.Collections.Generic;
using System.Text;

namespace MusicBrowser.LibraryManagement
{
   
    public class PlayList : ModelItem
    {
        public  PlayList()
        {
            _status = RunningStatus.Stopped;
          
        }

       

        private StringBuilder pl;

        
        public void Start(FolderItemListMCE item)
        {

            pl = new StringBuilder(@"#EXTM3U
");
            if (Status == RunningStatus.Stopped)
            {
                Status = RunningStatus.Started;
                string path = item.folderItems.Path;
                                
               Microsoft.MediaCenter.UI.Application.DeferredInvokeOnWorkerThread(RunTest,TestFinished,path);
            }
        }


        
        //
        // Our current running state
        // 
        public RunningStatus Status
        {
            get { return _status; }
            set
            {
                if (_status != value)
                {
                    _status = value;
                    FirePropertyChanged("Status");
                }
            }
        }

       
       

        private void TestFinished(object obj)
        {
            if (!IsDisposed)
            {
                Status = RunningStatus.Stopped;

               string playlistPath = System.IO.Path.GetTempPath() + "playlist.m3u";
                System.IO.File.WriteAllText(playlistPath, pl.ToString(), Encoding.Default);
                MediaCenterEnvironment mce;
                mce = Microsoft.MediaCenter.Hosting.AddInHost.Current.MediaCenterEnvironment;
                mce.PlayMedia(Microsoft.MediaCenter.MediaType.Audio, playlistPath, false);
                mce.MediaExperience.GoToFullScreen();
                
            }
        }


        private void RunTest(object obj)
        {
            

            ThreadPriority priority = Thread.CurrentThread.Priority;
            Thread.CurrentThread.Priority = ThreadPriority.Lowest;

            string path = Convert.ToString(obj);
          
            try
            {

                // Start our stopwatch to time the amount of time we spend here
              
                
                           
                    // Microsoft.MediaCenter.UI.Application.DeferredInvoke(TestingCandidate, candidate);

                    
                    foreach (string fileName in Directory.GetFiles(path, "*.*"))
                    {
                        if (Status != RunningStatus.PendingStop && Helper.IsVideo(fileName))
                        {
                            
                            pl.AppendLine(fileName);
                        // Microsoft.MediaCenter.UI.Application.DeferredInvoke(PrimeFound, candidate);
                        }

                    }
                    // Add all directories at this directory.
                    string[] subdirEntries = Directory.GetDirectories(path);
                    foreach (string directoryName in subdirEntries)
                    {

                                try
                                {
                                    DirectoryInfo info = new DirectoryInfo(directoryName);
                                    if ((info.Attributes & FileAttributes.Hidden) != FileAttributes.Hidden)
                                    RunTest(directoryName);

                                }
                                catch (UnauthorizedAccessException) { }
                            }

                            
            }
            finally
            {
                //
                // Reset our thread's priority back to its previous value
                //
                Thread.CurrentThread.Priority = priority;
            }
        }


        public enum RunningStatus
        {
            Started,
            PendingStop,
            Stopped
        }

        private volatile RunningStatus _status;
   
    }

  
}
